<template>
  <el-dialog
    :title="!dataForm.messageId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="信息标题" prop="messageTitle">
      <el-input v-model="dataForm.messageTitle" placeholder="信息标题"></el-input>
    </el-form-item>
    <el-form-item label="信息内容" prop="messageDesc">
      <el-input v-model="dataForm.messageDesc" placeholder="信息内容"></el-input>
    </el-form-item>
    <el-form-item label="1经纪人消息
2经纪人公告
3经纪人求组
4经纪人求购
" prop="messageType">
      <el-input v-model="dataForm.messageType" placeholder="1经纪人消息
2经纪人公告
3经纪人求组
4经纪人求购
"></el-input>
    </el-form-item>
    <el-form-item label="经纪人id" prop="brokerId">
      <el-input v-model="dataForm.brokerId" placeholder="经纪人id"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          messageId: 0,
          messageTitle: '',
          messageDesc: '',
          messageType: '',
          brokerId: ''
        },
        dataRule: {
          messageTitle: [
            { required: true, message: '信息标题不能为空', trigger: 'blur' }
          ],
          messageDesc: [
            { required: true, message: '信息内容不能为空', trigger: 'blur' }
          ],
          messageType: [
            { required: true, message: '1经纪人消息
2经纪人公告
3经纪人求组
4经纪人求购
不能为空', trigger: 'blur' }
          ],
          brokerId: [
            { required: true, message: '经纪人id不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.messageId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.messageId) {
            this.$http({
              url: this.$http.adornUrl(`/message/message/info/${this.dataForm.messageId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.messageTitle = data.message.messageTitle
                this.dataForm.messageDesc = data.message.messageDesc
                this.dataForm.messageType = data.message.messageType
                this.dataForm.brokerId = data.message.brokerId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/message/message/${!this.dataForm.messageId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'messageId': this.dataForm.messageId || undefined,
                'messageTitle': this.dataForm.messageTitle,
                'messageDesc': this.dataForm.messageDesc,
                'messageType': this.dataForm.messageType,
                'brokerId': this.dataForm.brokerId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
